package mapReduce.demo05_shuffle.demo06_groupGenderCount;

import mapReduce.writable.GroupGenderWritable;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

public class GroupGenderCountGroupingComparator extends WritableComparator {

    public GroupGenderCountGroupingComparator() {
        super(GroupGenderWritable.class,true);
    }

    @Override
    public int compare(WritableComparable a, WritableComparable b) {
        GroupGenderWritable aBean = (GroupGenderWritable) a;
        GroupGenderWritable bBean = (GroupGenderWritable) b;

        String aGroupName = aBean.getGroupName();
        String bGroupName = bBean.getGroupName();

        int aAge = aBean.getAge();
        int bAge = bBean.getAge();

        if(aGroupName.equals(bGroupName)){
            return aAge-bAge;
        }else{
            return aGroupName.compareTo(bGroupName);
        }
    }
}
